﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using NHibernate.Criterion;
using System.Text;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using ZLCMS.Business;
using System.IO;

namespace ZLCMS.Web.SystemManage.Basics.Log
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this) { RecordCount = GetLogCount() };
            tb_search_datebegin.Text = Request["datebegin"];
            tb_search_dateend.Text = Request["dateend"];
            if (_framePage.IsAjaxPagerPostBack)
            {
                BindGridLog(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                BindGridLog(1, 20);
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindGridLog(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysLogInfo>();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Desc("Id")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                IList<SysLogInfo> list = criteria.List<SysLogInfo>();
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    StringWriter sw = new StringWriter();
                    sw.WriteLine("时间\t线程ID\t等级\tLogger\t消息");
                    foreach (SysLogInfo log in list)
                    {
                        sw.WriteLine("'" + log.Date + "\t" +log.Thread+ "\t" + log.Level + "\t" +log.Logger + "\t"  +log.Message+ "\t" );

                        //if (log.Message.Length > 30)
                        //{
                        //    log.Message = log.Message.Substring(0, 30) + "...";
                        //}
                    }
                    sw.Close();

                    OutputKit.OutputExcel(Response, sw);
                }
                else
                {
                    foreach (SysLogInfo log in list)
                    {
                        log.Message = FormatUtil.FixMaxLength(log.Message, 30, "...");
                    }
                    gridLog.DataSource = list;
                    gridLog.DataBind();
                }
                
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetLogCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysLogInfo>();
                SetupCondition(ref criteria);
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }

        private void SetupCondition(ref ICriteria criteria)
        {
            if (tb_search_account.Text.Trim() != "")
            {
                criteria.Add(Expression.Like("Message", "%" + tb_search_account.Text + "%"));
            }

            if (tb_search_datebegin.Text.Trim() != "")
            {
                criteria.Add(Expression.Gt("Date", DateTime.Parse(tb_search_datebegin.Text)));
            }

            if (tb_search_dateend.Text.Trim() != "")
            {
                criteria.Add(Expression.Lt("Date", DateTime.Parse(tb_search_dateend.Text).AddHours(23).AddMinutes(59).AddSeconds(59)));
            }

        }
    }
}
